package com.wandian.view.components.ui
{
	import com.greensock.TweenLite;
	import com.wandian.data.GameData;
	import com.wandian.util.CLZButton;
	import com.wandian.util.RadioManage;
	import com.wandian.view.components.ui.avatar.SK_CursorMove;
	import com.wandian.view.components.ui.replay.PublicCard;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;

	/**
	 * @author long
	 * @Create  2013-2-28 下午02:55:41
	 * @QQ 108232706
	 * @version 1.0
	 */
	public class GR extends Sprite
	{
		private var _cursorMove:MovieClip;
		private var dx:int;
		private var dy:int;
		private var dp:DatePicker;
		private var _mcGameRecord:MovieClip;
		private var mcGameMain:MovieClip;
		private var _mcGameRecordList:MovieClip;
		private var _radioManage:RadioManage

		private var _mcPublicCard:PublicCard;
		public function GR()
		{
			//			trace("-----------");
			addEventListener(Event.ADDED_TO_STAGE, initGR);
		}

		protected function initGR(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, this.initGR);
			addGRMain();
			addGameRecordList();
			addDatePick();
			addMoveCursor();
//			AddBtn.initBtn(this._mcGameRecord, "", AddBtn.CLOSE_TWO, "", 4, 1);
			var btn:DisplayObject=AddBtn.initFunBtn(this._mcGameRecord, onClose, AddBtn.CLOSE_TWO, "", 2, 3);
			btn.name="btn_Close";
			mcGameMain=_mcGameRecord;
			_mcPublicCard=new PublicCard(this.mcGameMain.publicCard_mc);
		}

		private function onClose(event:MouseEvent):void
		{
			this.visible=!this.visible;
		}

		private function addGameRecordList():void
		{
			_mcGameRecordList=GameData.getMovieClip("GameRecordList");
			addChild(_mcGameRecordList);
			this._mcGameRecordList.x=-_mcGameRecordList.width - 10;
			this._mcGameRecordList.y=-20;
			_mcGameRecordList.txt_Name.addEventListener(MouseEvent.CLICK, onChangeDateHandler);
			_mcGameRecordList.radio1.stop();
			_mcGameRecordList.radio2.stop();
			_mcGameRecordList.radio3.stop();
			CLZButton.setButton(_mcGameRecordList.btn_search, "搜索");
			_mcGameRecordList.btn_search.stop();
			_mcGameRecordList.addEventListener(MouseEvent.ROLL_OUT, this.outMouseHandler);
			_radioManage=new RadioManage([[1, _mcGameRecordList.radio1], [2, _mcGameRecordList.radio2], [3, _mcGameRecordList.radio3]], true);
			_radioManage.addEventListener(Event.CHANGE, this.changeRadioHandler);
			_radioManage.setRadioSelect(1, true);
			_mcGameRecordList.visible=false;
		}

		private function windosViewRemove(display:DisplayObject):void
		{
			if (display != null)
			{
				display.visible=false;
			}
			this._mcGameRecord.btn_List.visible=true;
		}

		private function changeRadioHandler(event:Event):void
		{
			switch (this._radioManage._radioId)
			{
				case 1:
					break;
				default:
					break;
			}
			trace(this._radioManage._radioId);
		}

		private function outMouseHandler(event:MouseEvent):void
		{
			TweenLite.to(_mcGameRecordList, 0.4, {x: -_mcGameRecordList.width - 10, onComplete: this.windosViewRemove, onCompleteParams: [_mcGameRecordList]});
			return;
		}

		protected function onChangeDateHandler(event:MouseEvent):void
		{
			dp.x=10;
			dp.y=10;
			dp.show();
		}

		private function addGRMain():void
		{
			_mcGameRecord=GameData.getMovieClip("GameRecord");
			addChild(_mcGameRecord);
			GameData.stopMcAllChild(_mcGameRecord);
			_mcGameRecord.btn_List.addEventListener(MouseEvent.CLICK, onGRListHandler);
			CLZButton.setButton(this._mcGameRecord.btn_List, "牌局列表");
		}

		private function onGRListHandler(event:MouseEvent):void
		{
			if (this._mcGameRecord.btn_List.visible)
			{
				_mcGameRecordList.visible=true;
				this._mcGameRecord.btn_List.visible=false;
				TweenLite.to(_mcGameRecordList, 0.5, {x: -3});
			}
			else
			{
				TweenLite.to(_mcGameRecordList, 0.4, {x: -_mcGameRecordList.width - 10, onComplete: this.windosViewRemove, onCompleteParams: [_mcGameRecordList]});

			}

		}

		private function addDatePick():void
		{
			dp=new DatePicker();
			dp.firstDayOfWeek=1;
			dp.showToday=true;
			dp.addEventListener(Event.SELECT, onSelect);
			dp.addEventListener(Event.SCROLL, onScroll);
			_mcGameRecordList.addChild(dp);
			dp.hide();
		}

		private function onScroll(event:Event):void
		{
			trace("scroll");
		}

		private function onSelect(event:Event):void
		{
			trace(dp.selectDate);
			dp.hide();
		}

		private function addMoveCursor():void
		{
			this._cursorMove=new SK_CursorMove();
			this._cursorMove.visible=false;
			this._cursorMove.mouseEnabled=false;
			this.addChild(this._cursorMove);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}

		private function onMouseDown(event:MouseEvent):void
		{
			if (this._mcGameRecordList.hitTestPoint(stage.mouseX, stage.mouseY))
				return;
			if (this._mcGameRecord.btn_List.hitTestPoint(stage.mouseX, stage.mouseY))
				return;
			if (this.dp.hitTestPoint(stage.mouseX, stage.mouseY))
				return;
			if (this._mcGameRecord.getChildByName("btn_Close").hitTestPoint(stage.mouseX, stage.mouseY))
				return;
			this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			//			_mcPanel.addEventListener(MouseEvent.MOUSE_OUT, onMouseUp);
			this._cursorMove.visible=true;
			//			this._cursorScale.visible = false;
			this._cursorMove.x=this.mouseX;
			this._cursorMove.y=this.mouseY;
			dx=this.x - this.mouseX;
			dy=this.y - this.mouseY;
			Mouse.hide();
		}

		private function onMouseMove(event:MouseEvent):void
		{
			this.x=this.mouseX + dx;
			this.y=this.mouseY + dy;
			this._cursorMove.x=this.mouseX;
			this._cursorMove.y=this.mouseY;
		}

		private function onMouseUp(event:MouseEvent):void
		{
			this.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			//			_mcPanel.removeEventListener(MouseEvent.MOUSE_OUT, onMouseUp);
			this.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			this._cursorMove.visible=false;
			Mouse.show();
		}
	}
}
